#!/usr/bin/env perl

# Usage: ./migrate.pl fn1.csv fn2.csv > data.json

use strict;
use Text::CSV::Slurp;
use JSON::XS;

for my $fn (@ARGV) {
	open my $fh, '<', $fn;
	my $csv_text = do { local $/; <$fh>; };
	$csv_text =~ s/\\N/""/g; # null fields

	# FIXME: binary mode does not seem to handle embedded line breaks
	# To cope, we replace embedded line breaks with a vertical bar.
	$csv_text =~ s/\r\\\n/|/gs;

	my $csv_lines = [];
	$csv_lines = Text::CSV::Slurp->load(string => $csv_text, (
		  allow_whitespace => 1
		, binary => 1
		, escape_char => "\\"
		, empty_is_undef => 1
	)) or die 'Text/CSV/Slurp died';

	my $json_text = '';
	$json_text = JSON::XS->new->ascii->pretty->allow_nonref->encode($csv_lines)
		or die 'JSON/XS died';

	print $json_text, "\n";
}

exit;
